

import os.path
import time
from datetime import datetime




def read_file(file_path):
    file = open( file_path , encoding='utf-8')
    # 此时只读取了一行
    pre_line = file.readline()
    pre_line = pre_line.replace("\n", "")

    time_gap = list()
    try:
        while pre_line:
            next_line = file.readline()
            next_line = next_line.replace("\n", "")
            # 计算时间间隔:  2024:03:19 10:00:03
            pre_line_date = datetime.strptime(pre_line, '%Y:%m:%d %H:%M:%S')
            next_line_date = datetime.strptime(next_line, '%Y:%m:%d %H:%M:%S')
            gap = (next_line_date - pre_line_date)
            # print(f"--{next_line_date}-----{pre_line_date}---- {gap}")
            time_gap.append(dict(time=f"{next_line_date}" , gap=f"{gap.seconds}"))
            pre_line = next_line
        file.close()
    except Exception as e:
        print(f"-----error: {e}")
        pass

    # time_gap.sort()
    # sorted(time_gap,key=lambda x: x["gap"], reverse=True)

    aaa = [item for item in time_gap if int(item["gap"]) > 10]
    for i in aaa:
        print(f"--{i}---")



read_file("log_2024_03_27.txt")
read_file("log_2024_03_28.txt")
read_file("log_2024_03_29.txt")
read_file("log_2024_03_30.txt")
read_file("log_2024_03_31.txt")
read_file("log_2024_04_01.txt")
